<?php
class CMRPageShowCategory extends CMRPage
{
	private $category;
//	private $categoryName;

	
	private $displayLimit = 20;
	private $displayStart = 0;
	private $displayLetter;
	
	public function __construct($database)
	{
		parent::__construct($database);
		
		if(isset($_POST['CategorySearchLetterLetter']))
			$this->displayLetter=$_POST['CategorySearchLetterLetter'];
		
		if(isset($_POST['CategorySearchLimitNumber']))
			$this->displayLimit=$_POST['CategorySearchLimitNumber'];
			
		if(isset($_POST['CategorySearchLimitStart']))
			$this->displayStart=$_POST['CategorySearchLimitStart'];
			
		if(isset($_POST['Category']))
		{
			$this->category = new Category($database->Recopy(),$_POST['Category']);
		}
			
			
			
	}
	
	protected function getContentsDiv()
	{
		$resourceID = new Hidden();
		$resourceID->setName("ResourceID");
		$html = $resourceID->getHTML();
	
/*		$category = new Hidden();
		$category->setName("Category");
		$category->setValue($_POST['Category']);
		$html .= $category->getHTML();*/
	
		$html .= "<div id=\"content\">\n";
		$html .= "<h1>".$this->category->getCategoryName()."</h1>\n";
		
//		echo substr(strval($this->category->getCategoryID()),0,1);
		
		$sectionsPanel = new SectionsPanel(substr($this->category->getCategoryID(),0,1));
		$html .= $sectionsPanel->getHTML();
		
		
		$html .= $this->category->getNavigationHTML();
		
		

//		$html .= "<p> This section contains the resources contained in the ".$this->category->getCategoryName()." category. Choose a resource from the list below.</p>\n";

		if(($this->userGroup=="Editors")||($this->userGroup=="Supervisors"))
		{
			$hidden = new Hidden();
			$hidden->setName("SubAction");
			$html .= $hidden->getHTML();

/*			$hidden = new Hidden();
			$hidden->setName("ResourceID");
			$html .= $hidden->getHTML();
*/		

		}
		
		$panel = new AlphabetSelectPanel();
		$panel->setName("CategorySearchLetter");
		$panel->setDisplayLetter($this->displayLetter);
		$html .= $panel->getHTML();
		
		$this->category->setDisplayLetter($this->displayLetter);
		$this->category->setDisplayStart($this->displayStart);
		$this->category->setDisplayLimit($this->displayLimit);
		$this->category->setPage($this);
		
		$resources = $this->category->getResources();
		
		if(count($resources == 0))
		{
			$this->displayStart -= $this->displayLimit;
			if($this->displayStart<0)$this->displayStart=0;
			$this->category->setDisplayStart($this->displayStart);
			$resources = $this->category->getResources();
		}
		
		$panel = new DisplayLimitPanel();
		$panel->setName("CategorySearchLimit");
		$panel->setLimit($this->displayLimit);
		$panel->setStart($this->displayStart);
//		echo "Display limit = ".$this->displayLimit."<br>\n";
//		echo "Display start = ".$this->displayStart."<br>\n";
		$html .= $panel->getHTML();
		
/*		echo "count($resources) = ".count($resources)."<br>\n";
		echo "displayStart = ".$this->displayStart."<br>\n";
		echo "numResources = ".$this->category->getNumResources()."<br>\n";
		echo "numTotalResources = ".$this->category->getNumTotalResources()."<br>\n";
*/		
		if(count($resources)==0)$html .= "<p>There are currently no resources in this category</p>\n";
		foreach ($resources as $resource)
		{
			$html .= $resource->getSummaryHTML();
		}
				
		$html .= "</div>\n";
		return $html;
	}
	
}